using Test
using TongLun

@testset "ComplexF32 Support Tests" begin
    
    @testset "ComplexF32 Type Preservation" begin
        TongLun.export_fn("test.identity", identity)
        
        # Test ComplexF32 round-trip
        z32 = ComplexF32(1.5f0, 2.5f0)
        result = TongLun.call_fn("test.identity", z32)
        @test result isa ComplexF32
        @test result === z32
        
        # Test ComplexF64 for comparison
        z64 = ComplexF64(1.5, 2.5)
        result64 = TongLun.call_fn("test.identity", z64)
        @test result64 isa ComplexF64
        @test result64 === z64
    end
    
    @testset "ComplexF32 Operations" begin
        TongLun.export_fn("test.complex_conj", conj)
        TongLun.export_fn("test.complex_abs", abs)
        TongLun.export_fn("test.complex_real", real)
        TongLun.export_fn("test.complex_imag", imag)
        
        z32 = ComplexF32(3.0f0, 4.0f0)
        
        # Test conjugate preserves ComplexF32 type
        conj_result = TongLun.call_fn("test.complex_conj", z32)
        @test conj_result isa ComplexF32
        @test conj_result ≈ ComplexF32(3.0f0, -4.0f0)
        
        # Test abs returns Float64 (expected behavior)
        abs_result = TongLun.call_fn("test.complex_abs", z32)
        @test abs_result isa Float64
        @test abs_result ≈ 5.0
        
        # Test real/imag return Float64 (TongLun limitation)
        real_result = TongLun.call_fn("test.complex_real", z32)
        @test real_result isa Float64
        @test real_result ≈ 3.0
        
        imag_result = TongLun.call_fn("test.complex_imag", z32)
        @test imag_result isa Float64
        @test imag_result ≈ 4.0
    end
    
    @testset "ComplexF32 Arrays" begin
        TongLun.export_fn("test.array_sum", sum)
        TongLun.export_fn("test.array_length", length)
        
        # ComplexF32 array
        arr_c32 = ComplexF32[1.0f0+2.0f0im, 3.0f0+4.0f0im, 5.0f0+6.0f0im]
        
        # Test array operations
        @test TongLun.call_fn("test.array_length", arr_c32) === 3
        
        result_sum = TongLun.call_fn("test.array_sum", arr_c32)
        @test result_sum isa ComplexF32
        @test result_sum ≈ ComplexF32(9.0f0, 12.0f0)
    end
    
    println("✅ All ComplexF32 tests passed!")
end 